//
//  LCTabBar.m
//  04 -自定制tabBar
//
//  Created by 千锋 on 16/5/7.
//  Copyright (c) 2016年 千锋. All rights reserved.
//

#import "LCTabBar.h"
#import "LCButton.h"




@implementation LCTabBar

#pragma mark - 创建视图控制器对应的按钮
//保证每次添加视图控制器的时候,为这个视图控制器创建一个对应得按钮
- (void)setItem:(UITabBarItem *)item{
    
    _item = item;
    
    //==========创建对应的按钮==========
    //创建按钮,不需要设置frame
    LCButton *button = [[LCButton alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];
    
    //设置属性
    button.title = item.title;
    button.image = item.image;
    button.selectedImage = item.selectedImage;
    [button addTarget:self action:@selector(changeController:)];
    [button setTitleFont:[UIFont systemFontOfSize:14] color:[UIColor grayColor]];
    
    //显示在界面上
    [self addSubview:button];
    
    
    
}
#pragma mark - 按钮点击,切换视图控制器
- (void)changeController:(LCButton *)button{
    //获取之前选中的按钮
    LCButton *tBtn = (LCButton *)[self viewWithTag:_selectedIndex + 1];
    
    //将选中的变成非选中
    tBtn.isSelected = NO;
    
    //将当前点击的按钮变成选中
    button.isSelected = YES;
    //更新选中下标
    _selectedIndex = button.tag - 1;
    //告诉代理切换视图控制器
    [self.delegate changeCurrentController];
    
}

#pragma mark - 计算子视图的frame
- (void)layoutSubviews{
    [super layoutSubviews];
    
    if (_centerView) {
        [self calculateButtonFrameWithCenterView];
    }else{
        [self calculateButtonFrameWithoutCenterView];
    }
    
}
#pragma mark - 计算中间视图
- (void)calculateButtonFrameWithCenterView{
    //计算中间视图的frame
    CGFloat centerW = _centerView.frame.size.width;
    CGFloat centerH = _centerView.frame.size.height;
    CGFloat centerX = self.center.x - centerW/2.0f;
    CGFloat centerY = (self.frame.size.height - centerH)/2.0f;
    _centerView.frame = CGRectMake(centerX, centerY, centerW, centerH);
    //计算按钮的frame
    CGFloat buttonW = (self.frame.size.width - centerW)/(self.subviews.count - 1);
    CGFloat buttonH = self.frame.size.height - 2;
    CGFloat buttonY = 0;
    int i1 = 0;
    for (UIView *view in self.subviews) {
        if (i1 < self.subviews.count/2) {
            
            if ([view isKindOfClass:[LCButton class]]) {
                //创建按钮的位置
                CGFloat x = i1 *buttonW;
                view.frame = CGRectMake(x, buttonY, buttonW, buttonH);
                view.tag = i1 + 1;
                //根据选中下标设置按钮是否选中
                if (i1 == _selectedIndex) {
                    LCButton *tview = (LCButton *)view;
                    tview.isSelected = YES;
                }
                i1 ++;
            }
        }else{
            if ([view isKindOfClass:[LCButton class]]) {
                CGFloat x = i1 * buttonW + centerW;
                view.frame = CGRectMake(x, buttonY, buttonW, buttonH);
                view.tag = i1 + 1;
                //根据选中下标设置按钮是否选中
                if (i1 == _selectedIndex) {
                    LCButton *tview = (LCButton *)view;
                    tview.isSelected = YES;
                }
                i1 ++;
            }
        }
    }
  
}
//没有中间视图的时候计算按钮的frame
- (void)calculateButtonFrameWithoutCenterView{
    //计算所有按钮的frame
    CGFloat buttonW = self.frame.size.width / self.subviews.count;
    CGFloat buttonH = self.frame.size.height - 2;
    CGFloat buttonY = 0;
    int i = 0;
    
    //1，获取所有的按钮
    for (LCButton *button in self.subviews) {
        CGFloat buttonX = i * buttonW;
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        button.tag = i + 1;
        
        //根据选中下标设置按钮是否选中
                if (i   == _selectedIndex) {
                    button.isSelected = YES;
                }
        i++;
    }

}
#pragma mark - 重写中间视图set方法加在界面上
- (void)setCenterView:(UIView *)centerView{
    _centerView = centerView;
    [self addSubview:_centerView];
}
@end
